#ifndef _SETTING_
#define _SETTING_

#include <FS.h> 
#include <ArduinoJson.h>
#include <LittleFS.h>
#include "img.h"

#define Key_Time "time"

#define Key_WifiName    "wifiName"
#define Key_WifiPwd     "wifiPwd"
#define Key_WifiAutoConn  "wifiAutoConn"

#define Key_MqttPath    "mqttPath"
#define Key_MqttPort    "mqttPort"
#define Key_MqttName    "mqttName"
#define Key_MqttPwd     "mqttPwd"
#define Key_MqttAutoConn  "mqttAutoConn"

#define Key_WaitTime  "drink_waitTime"
#define Key_OutTime_1 "drink_out1Time"
#define Key_OutTime_2 "drink_out2Time"
#define Key_OutTime_3 "drink_out3Time"
#define Key_OutTime_4 "drink_out4Time"
#define Key_ChangePeriod "changePeriod"
#define Key_ChangeTime "changeTime"

#define Ctl_Reboot  "reboot"
#define Ctl_WifiConn  "wifiConn"
#define Ctl_WifiDisconn  "wifiDisConn"

class Setting
{
public:
  bool   wifi_bootConn; //开机连接Wifi标志位
  String wifi_ssid; //wifi名称
  String wifi_pwd;  //wifi密码

  bool   mqtt_bootConn;   //开机连接mqtt服务器标志位
  String mqtt_addr; //mqtt服务器地址
  String mqtt_port; //mqtt端口号
  String mqtt_name; //用户名
  String mqtt_pwd;  //密码
  
  bool log_outFlag = 0;      //输出日志信息标志位

  int ui_period = 30;  //切换周期 单位：s
  int ui_switch = 20;   //分割时间 单位：s，小于切换周期

  int drink_waitTime = 30;   //喝水间隔时间min
  int drink_out1Time = 30;  //超时1未喝水状态维持时间
  int drink_out2Time = 60;
  int drink_out3Time = 60;
  int drink_out4Time = 60;

  //系统界面显示的图像指针设置，目前全部使用系统的图片，后续需要改为存储中的图片（可不被修改、串口设置保存的）
  unsigned char *logImg  = (unsigned char *)img_init2; //产品log，可以是公司图标
  unsigned char *initImg = (unsigned char *)img_init;  //
  
  //================= 对本对象的变量设置接口 =========================
  bool setValue(String key,String value)
  {
    if(key == "")  return 0;
    else if(key == Key_WifiName)     wifi_ssid = value;
    else if(key == Key_WifiPwd)      wifi_pwd = value;
    else if(key == Key_WifiAutoConn) wifi_bootConn = !(value.toInt() == 0);
    
    else if(key == Key_MqttPath)     mqtt_addr = value; //mqtt服务器地址
    else if(key == Key_MqttPort)     mqtt_port = value; //mqtt端口号
    else if(key == Key_MqttName)     mqtt_name = value; //用户名
    else if(key == Key_MqttPwd)      mqtt_pwd = value;  //密码
    else if(key == Key_MqttAutoConn) mqtt_bootConn = !(value.toInt() == 0);   //开机连接mqtt服务器标志位
    
    else if(key == Key_WaitTime)  drink_waitTime = value.toInt(); 
    else if(key == Key_OutTime_1) drink_out1Time = value.toInt(); 
    else if(key == Key_OutTime_2) drink_out2Time = value.toInt(); 
    else if(key == Key_OutTime_3) drink_out3Time = value.toInt(); 
    else if(key == Key_OutTime_4) drink_out4Time = value.toInt(); 

    else if(key == Key_ChangePeriod) ui_period = value.toInt(); 
    else if(key == Key_ChangeTime)   ui_switch = value.toInt(); 
    
    return 1;
  }
  String getValue(String key)
  {
    if(key == "") return "No key/value";

    else if(key == Key_WifiName)  return wifi_ssid;
    else if(key == Key_WifiPwd)   return wifi_pwd;
    else if(key == Key_WifiAutoConn) return String(wifi_bootConn);
    
    else if(key == Key_MqttPath)     return mqtt_addr; 
    else if(key == Key_MqttPort)     return mqtt_port; 
    else if(key == Key_MqttName)     return mqtt_name; 
    else if(key == Key_MqttPwd)      return mqtt_pwd;    
    else if(key == Key_MqttAutoConn) return String(mqtt_bootConn); 
    
    else if(key == Key_WaitTime) return String(drink_waitTime) + "min"; 
    else if(key == Key_OutTime_1) return String(drink_out1Time) + "min";  
    else if(key == Key_OutTime_2) return String(drink_out2Time) + "min";  
    else if(key == Key_OutTime_3) return String(drink_out3Time) + "min";  
    else if(key == Key_OutTime_4) return String(drink_out4Time) + "min"; 
    else return "No key/value";
  }
  bool init()
  {
    return initConfig();
  }
public:
  //================= 对内的内存操作，需要持续化到rom ===============
  bool initConfig()  //初始化函数
  {
    return LittleFS.begin();
  }

  //wifi设置
  bool saveToFile()
  {
    StaticJsonDocument<1024> doc;
    doc["wifi_bootConn"] = wifi_bootConn;
    doc["wifi_ssid"] = wifi_ssid;
    doc["wifi_pwd"] = wifi_pwd;

    doc["mqtt_bootConn"] = mqtt_bootConn;
    doc["mqtt_addr"] = mqtt_addr;
    doc["mqtt_port"] = mqtt_port;
    doc["mqtt_name"] = mqtt_name;
    doc["mqtt_pwd"] = mqtt_pwd;
    
    doc["log_outFlag"] = log_outFlag;

    File configFile = LittleFS.open("/config.json", "w");
    if (!configFile) {
      return false;
    }
    serializeJson(doc, configFile);
    return true;
  }
  
  //读取文件中的参数到
  bool readFromFile()
  {
    /// 打开文件
    File configFile = LittleFS.open("/config.json", "r");
    if (!configFile) 
      return false;
    
    /// 确定读取大小
    size_t size = configFile.size();
    if (size > 1024) 
      return false;
    
    /// 读取
    std::unique_ptr<char[]> buf(new char[size]);
    configFile.readBytes(buf.get(), size);

    /// 转json
    StaticJsonDocument<1024> doc;
    auto error = deserializeJson(doc, buf.get());
    if (error) 
      return 0;
    
    /// 解析
    wifi_bootConn = doc["wifi_bootConn"];
    const char *buff2 = doc["wifi_ssid"];
    wifi_ssid = String(buff2);
    const char *buff3 = doc["wifi_pwd"];
    wifi_pwd = String(buff3);
    
    mqtt_bootConn = doc["mqtt_bootConn"];
    const char *buff5 = doc["mqtt_addr"];
    mqtt_addr = String(buff5);
    const char *buff6 = doc["mqtt_port"];
    mqtt_port = String(buff6);
    const char *buff7 = doc["mqtt_name"];
    mqtt_name = String(buff7);
    const char *buff8 = doc["mqtt_pwd"];
    mqtt_pwd = String(buff8);

    log_outFlag = doc["log_outFlag"];

    return 1;
  }
  
  //时间设置
  bool setTime(){}
  //喝水提醒时间设置
  //切换周期设置
  //切换时间
};

Setting setting;        //系统配置



#endif
